﻿using AutoMapper;
using ManagementApi.Models.System.Flow;
using Modules;
using PmSoft;
using PmSoft.Entity;
using PmSoft.Logging;

namespace ManagementApi.Models.System.TableLog
{
    public class SysTableLogDto
    {
        /// <summary>
        /// 日志主键
        ///</summary>
        public int Id { get; set; }

        /// <summary>
        /// 操作类型
        ///</summary>
        public EntityOperationType OperationType { get; set; }

        public string OperationTypeName { get; set; }

        /// <summary>
        /// 数据库名
        ///</summary>
        public string DatabaseName { get; set; }

        /// <summary>
        /// 表名
        ///</summary>
        public string TableName { get; set; }

        /// <summary>
        /// 对象ID
        ///</summary>
        public string ObjectId { get; set; }

        /// <summary>
        /// 操作数据
        ///</summary>
        public string Data { get; set; }

        /// <summary>
        /// 操作者安全信息ID
        ///</summary>
        public int? SecurityId { get; set; }

        /// <summary>
        /// 操作用户ID
        ///</summary>
        public int UserId { get; set; }

        /// <summary>
        /// 操作用户类型
        ///</summary>
        public UserType UserType { get; set; }

        public string UserTypeName { get; set; }

        /// <summary>
        /// 操作者名称
        ///</summary>
        public string Operator { get; set; }

        /// <summary>
        /// 操作IP
        ///</summary>
        public string Ip { get; set; }

        /// <summary>
        /// 操作IP归属地
        /// </summary>
        public string IpGeo { get; set; }

        /// <summary>
        /// 操作URL
        ///</summary>
        public string Url { get; set; }

        /// <summary>
        /// 创建时间
        ///</summary>
        public DateTime CreateTime { get; set; }
    }

    public static partial class SysTableLogExceptions
    {
        public static SysTableLogDto AsDto(this SysTableLog entity)
        {
            SysTableLogDto dto = new();
            var mapper = ServiceAccessor.GetService<IMapper>();
            mapper.Map(entity, dto);
            dto.OperationTypeName = entity.OperationType.GetDescription();
            dto.UserTypeName = entity.UserType.GetDescription();
            return dto;
        }

        public static IEnumerable<SysTableLogDto> AsDtos(this IEnumerable<SysTableLog> entitys)
        {
            List<SysTableLogDto> dtos = new();
            foreach (var entity in entitys)
            {
                dtos.Add(entity.AsDto());
            }
            return dtos;
        }
    }
}